package com.egao.common.module.cooperative.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.Constants;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.company.service.CompanyService;
import com.egao.common.module.cooperative.constants.CooperativeProjectConstants;
import com.egao.common.module.cooperative.entity.CooperativeProject;
import com.egao.common.module.cooperative.entity.CooperativeProjectRecord;
import com.egao.common.module.cooperative.entity.vo.CooperativeProjectListVO;
import com.egao.common.module.cooperative.mapper.CooperativeProjectMapper;
import com.egao.common.module.cooperative.service.CooperativeProjectRecordService;
import com.egao.common.module.cooperative.service.CooperativeProjectService;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 产教融合项目服务接口实现
 * @author root
 */
@Service
public class CooperativeProjectServiceImpl extends ServiceImpl<CooperativeProjectMapper, CooperativeProject> implements CooperativeProjectService {

    @Autowired
    private CooperativeProjectRecordService cooperativeProjectRecordService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserService userService;

    @Override
    public List<CooperativeProjectListVO> listPage(PageParam<CooperativeProject> page) {
        return baseMapper.listPage(page);
    }

    @Override
    public JsonResult addCooperativeProject(CooperativeProject cooperativeProject) {
        cooperativeProject.setState(CooperativeProjectConstants.CP_STATE_PROCEED);
        cooperativeProject.setHits(0);
        if (baseMapper.insert(cooperativeProject) > 0) {
            cooperativeProjectRecordService.add(new CooperativeProjectRecord(cooperativeProject.getId(),
                    null,
                    CooperativeProjectConstants.RR_SCHOOL,
                    CooperativeProjectConstants.RC_PROJECT_INSERT));
            return JsonResult.ok("添加成功");
        }
        return JsonResult.ok("添加失败");
    }

    @Override
    public JsonResult updateCooperativeProject(CooperativeProject cooperativeProject) {
        if (baseMapper.updateById(cooperativeProject) > 0) {
            cooperativeProjectRecordService.add(new CooperativeProjectRecord(cooperativeProject.getId(),
                    null,
                    CooperativeProjectConstants.RR_SCHOOL,
                    CooperativeProjectConstants.RC_PROJECT_UPDATE));
            return JsonResult.ok("修改成功");
        }
        return JsonResult.ok("修改失败");
    }

    @Override
    public JsonResult deleteCooperativeProject(Integer id) {
        UpdateWrapper<CooperativeProject> uw = new UpdateWrapper<>();
        uw.set("deleted", Constants.DELETED_FALSE);
        uw.lambda().eq(CooperativeProject::getId, id);
        if (update(uw)) {
            cooperativeProjectRecordService.add(new CooperativeProjectRecord(id,
                    null,
                    CooperativeProjectConstants.RR_SCHOOL,
                    CooperativeProjectConstants.RC_PROJECT_DELETE));
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult detail(Integer id, String role) {
        if (null == id) {
            return JsonResult.error("参数异常");
        }

        CooperativeProject cooperativeProject = getById(id);
        if (null == cooperativeProject) {
            return JsonResult.error("项目不存在");
        }

        Company company = companyService.getById(cooperativeProject.getCompanyId());
        if (company != null) {
            cooperativeProject.setCompanyName(company.getName() + "(" + company.getOrganizationCode() + ")");
        }

        User user = userService.getById(cooperativeProject.getTeacherId());
        if (user != null) {
            cooperativeProject.setTeacherName(user.getName() + "(" + user.getUsername() + ")");
        }

        // 记录
        cooperativeProjectRecordService.add(new CooperativeProjectRecord(id,
                null,
                role,
                CooperativeProjectConstants.RC_PROJECT_VIEW));

        return JsonResult.ok().setData(cooperativeProject);
    }

    @Override
    public List<CooperativeProjectListVO> listPagePublicity(PageParam<CooperativeProject> page) {
        return baseMapper.listPagePublicity(page);
    }

    @Override
    public void addHits(Integer id) {
        addHits(getById(id));
    }

    @Override
    public void addHits(CooperativeProject cooperativeProject) {
        if (cooperativeProject != null) {
            int hits = cooperativeProject.getHits() != null ? cooperativeProject.getHits() + 1 : 0;
            cooperativeProject.setHits(hits);
            updateById(cooperativeProject);
        }
    }
}
